package com.me.template4j.controller;

import com.me.template4j.exception.CustomException;
import com.me.template4j.pojo.FileBean;
import com.me.template4j.pojo.ResultBean;
import com.me.template4j.service.DFSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
public class DFSController {

    @Autowired
    DFSService dfsService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping("/save")
    public ResultBean<String> save(MultipartFile file, String projectName) throws CustomException, IOException {
        String fileName = dfsService.saveFile(file, projectName);
        return new ResultBean(HttpStatus.OK,fileName,"上传成功");
    }

    /**
     * 文件查找
     */
    @GetMapping("/search")
    public ResultBean<List<FileBean>> search(String fileName, String projectName) {
        List<FileBean> fileBeans = dfsService.searchFile(fileName, projectName);
        return new ResultBean(HttpStatus.OK,fileBeans,"获取成功");
    }

    /**
     * 文件删除
     */
    @DeleteMapping("/del")
    public ResultBean del(String fileName, String projectName) {
        try {
            dfsService.deleteFile(fileName, projectName);
            return new ResultBean(HttpStatus.OK,null,"删除成功");
        } catch (CustomException e) {
            return new ResultBean(HttpStatus.INTERNAL_SERVER_ERROR,null,"删除失败");
        }
    }
}
